﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;

namespace SQT.tracnghiem.Ajax.ASP.Staff
{
    public partial class add_working : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string add_work = Request.QueryString["add_work"];

            DateTime last = DateTime.Now;
            var get_week = new TblWeekController().FetchAll().ToList();
            if (get_week.Count != 0)
            {
                var get_last = new TblWeekController().FetchAll().Last();
                last = DateTime.Parse(get_last.Monday.ToString()).AddDays(7);
            }

            int week = WeekOfYear(last);
            week--;

            new TblWeekController().Insert(week, last.Year, last,DateTime.Now);
            var last_week_id = new TblWeekController().FetchAll().Last();
            int last_week = last_week_id.Id;

            string[] split = new string[add_work.Split(',').Length];
            split = add_work.Split(',');

            for (int i = 0; i < split.Length-1; i++)
            {
                string[] split1 = new string[split[i].Split('_').Length];
                split1 = split[i].Split('_');
                int manager = int.Parse(split1[0]);

                string[] split2 = new string[7];
                split2 = split1[1].Split('|');
                new TblWorkingController().Insert(last_week, manager, split2[0], split2[1], split2[2], split2[3], split2[4], split2[5], split2[6], split2[7]);
            }

        }
        private int WeekOfYear(DateTime date)
        {
            // use the current culture
            System.Globalization.CultureInfo ci = System.Threading.Thread.CurrentThread.CurrentCulture;
            // use current culture's calendar
            System.Globalization.Calendar cal = ci.Calendar;
            // get the calendar week rule (i.e. what determines the first week in the year)
            System.Globalization.CalendarWeekRule cwr = ci.DateTimeFormat.CalendarWeekRule;
            // get the first day of week for the current culture
            DayOfWeek fdow = ci.DateTimeFormat.FirstDayOfWeek;
            // return the week
            return cal.GetWeekOfYear(date, cwr, fdow);
        }
    }
}